#include "StdAfx.h"

BTStatistics::BTStatistics(void) 
{
	this->_NumTrades = 0;
	this->_NumWins = 0;
	this->_InitialCapital = this->_EndingCapital = 10000;
	this->_MaxDrawdown = 0;
}

BTStatistics::~BTStatistics(void) { }

void BTStatistics::AddTrade(TYPE type, double price, DateTime dt)
{
	Trade newTrade;
	newTrade.Dt = dt;
	newTrade.Price = price;
	newTrade.Type = type;

	if(type == _SELL || type == _COVER)
	{
		this->_NumTrades++;
		
		if (type == _SELL) 
		{
			newTrade.Shares = this->_TradesBuySell.back().Shares;
			this->_EndingCapital += newTrade.Shares * price;
		
			if (this->_TradesBuySell.back().Price < price)
				this->_NumWins++;
		}
		else //cover 
		{
			newTrade.Shares = this->_TradesShortCover.back().Shares;
			this->_EndingCapital -= newTrade.Shares * price;
		
			if (this->_TradesShortCover.back().Price > price)
				this->_NumWins++;
		}			
	}
	else if(type == _BUY) {
		newTrade.Shares = this->_EndingCapital / price;
		this->_EndingCapital = 0;
	}
	else {//short
		newTrade.Shares = this->_TradesBuySell.back().Shares;
		this->_EndingCapital += newTrade.Shares * price; 
	}
	
	if (type == _BUY || type == _SELL) 
		this->_TradesBuySell.push_back(newTrade);
	else 
		this->_TradesShortCover.push_back(newTrade);
}